{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "540ff471-dcea-4b3e-9c0c-a3173f1c640e",
   "metadata": {},
   "source": [
    "# Tree Summarize"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "158b08a8-32d3-4397-ad37-75870416226b",
   "metadata": {},
   "source": [
    "## Load Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bbbac556-bb22-47e2-b8bf-80818d241858",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from llama_index import SimpleDirectoryReader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bc0d4087-1ee3-4c38-94c0-b34f87ea8aca",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "reader = SimpleDirectoryReader(\n",
    "    input_files=[\"../data/paul_graham/paul_graham_essay.txt\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7934bb4a-4c0f-4833-842f-7fd47e16eeae",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "docs = reader.load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "bf6b6f5c-5852-41be-8ce8-d94c520e0e50",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "text = docs[0].text"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efed56ee-fcd3-439c-a1b2-53c643f15c8e",
   "metadata": {},
   "source": [
    "## Summarize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e65c577c-215e-40e9-8f3f-c23a09af7574",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from llama_index.indices.response import TreeSummarize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "52c48278-f5b2-47bb-a240-6b66a191c6db",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "summarizer = TreeSummarize(verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "834ac725-54ce-4243-bc09-4a50e2590b28",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6 text chunks after repacking\n",
      "1 text chunks after repacking\n"
     ]
    }
   ],
   "source": [
    "response = await summarizer.aget_response(\"who is Paul Graham?\", [text])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a600aa73-74b8-4a20-8f56-1b273417f788",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Paul Graham is a computer scientist, writer, artist, entrepreneur, investor, and essayist. He is best known for his work in artificial intelligence, Lisp programming, and writing the book On Lisp, as well as for co-founding the startup accelerator Y Combinator and for his essays on technology, business, and start-ups. He is also the creator of the programming language Arc and the Lisp dialect Bel.\n"
     ]
    }
   ],
   "source": [
    "print(response)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
